import router from '@/router'

router.beforeEach(async(to, from, next) => {
  const token = localStorage.getItem('mytoken')
  const url = to.path
  const whiteList = ['/login', '/404']
  // 有 token  去登录页 => 首页
  if (token && url === '/login') {
    next('/')
  }

  // 有 token  不是去登录页 => 放行
  if (token && url !== '/login') {
    next()
  }
  // 没 token 在白名单whiteList => 放行
  if (!token && whiteList.includes(url)) {
    next()
  }
  // 没 token 不在白名单whiteList => 登录页
  if (!token && !whiteList.includes(url)) {
    next('/login')
  }
})
